package com.engine.budget.cronjob;

import com.engine.budget.enums.VerifyMessage;
import com.engine.budget.manager.InstructionManager;
import com.engine.budget.util.VerifyUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.file.Prop;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.integration.logging.Logger;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DY
 * @version 1.0
 * @date 2024/7/16 15:40
 * @description: 3.8.3浪潮折旧凭证写入执行数定时任务
 */
public class SynDepVoucherCronJob extends BaseCronJob {

    //凭证明细折旧表
    private String pzmxzjbtabname = "uf_pzmxzjb";

    private Logger logger = getLogger();

    @Override
    public void execute() {
        logger.setClassname(this.getClass().getCanonicalName());
        String nowTime = TimeUtil.getCurrentTimeString();
        String nowDate = TimeUtil.getCurrentDateString();

        try {
            logger.info("浪潮折旧凭证写入执行数定时任务...start,执行时间："+nowTime);

            RecordSet rs = new RecordSet();

            String sql = "select id, ledgername,accdocdate,amount,accdoccode,ledgercode " +
                    "from "+pzmxzjbtabname+" where  sfqs =''  or sfqs is null  and pzksm02 = '折旧凭证' ";
            boolean b = rs.executeQuery(sql);
            logger.info("浪潮折旧凭证写入执行数定时任务...查询sql:"+sql+",sqlFlag:"+b+",执行时间："+nowTime);

            //业务参数
            JSONArray paramsArr = new JSONArray();
            while (rs.next()) {
                Map<String, String> params = new HashMap<>();
                params.put("id", Util.null2String(rs.getString("id")));
                //预算项目
                params.put("budgetItem", Prop.getPropValue("businessbudgetpz", "zjysxmcode"));
                //预算科目
                params.put("budgetAccount", Prop.getPropValue("businessbudgetpz", "zjfykmcode"));
                //执行单位
                params.put("exeUnit", Util.null2String(rs.getString("ledgername")));

                String accdocdate = Util.null2String(rs.getString("accdocdate"));
                String year = accdocdate.length() > 4 ? accdocdate.substring(0, 4):"";
                //预算期间（年）
                params.put("budgetPeriod", year);
                //金额
                params.put("amount", Util.null2String(rs.getString("amount")));
                params.put("instruction", "JZX");
                //被占用单据号
                params.put("occDocNo", "");
                //执行单据号
                params.put("exeDocNo", Util.null2String(rs.getString("accdoccode")));
                //预算控制期
                params.put("budgetPostingPeriod", accdocdate);
                //执行日期
                params.put("executionDate", nowDate);
                //执行单据类型
                params.put("exeDocType", "总账单据");
                //报账人
                params.put("claimant", "");
                //来源系统
                params.put("sourceSystem", "浪潮");
                //说明
                params.put("remark", "");
                //报账部门
                params.put("reimbursementDept", Util.null2String(rs.getString("ledgername")));
                //报账公司
                params.put("reimbursementCompany", Util.null2String(rs.getString("ledgercode")));
                //经济事项
                params.put("economicEvent", "");
                //预算事项
                params.put("event", "");
            }

            for (int i = 0; i < paramsArr.size(); i++) {

                JSONObject data = paramsArr.getJSONObject(i);

                //参数必填验证
                Map<String, String> emptyMap = VerifyUtil.paramMustVeifty(data);
                Map<String, Object> checkMap = VerifyMessage.SUCCESS.fromMap();
                if(emptyMap.size() > 0) {
                    String emptyErrMessage = VerifyUtil.getEmptyErrMessage(emptyMap);
                    checkMap = VerifyMessage.MUST_EMPTY.setErrorMessage(emptyErrMessage).fromMap();
                    checkMap.put("status", -1);
                    checkMap.put("data", data);
                }

                InstructionManager.addInstructionLog(data, checkMap, rs);

                sql = "update "+pzmxzjbtabname+" set sfqs = '是' where id = ?";
                String id = data.getString("id");
                b = rs.executeUpdate(sql, id);

                logger.info("浪潮折旧凭证写入执行数定时任务...更新sql:"+sql+",sqlFlag:"+b+",id:"+id+",执行时间："+nowTime);
            }
        }catch (Exception e) {
            logger.error("浪潮折旧凭证写入执行数定时任务...异常err,"+e.getMessage()+",执行时间："+nowTime, e);
        }
        logger.info("浪潮折旧凭证写入执行数定时任务...end,执行时间："+nowTime);
    }


}
